﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using AutoMapper;

namespace Data 
{
    public class KhachHangRepository 
    {
        /// <summary>
        /// lấy danh sách toàn bộ bảng khách hàng
        /// </summary>
        /// <returns></returns>
 
        public List<KhachHangDTO> SelectAllKhachHangs() {
            List<KhachHangDTO> list = new List<KhachHangDTO>();
            QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();

            var query = from khItem in context.KhachHangList
                        select khItem;
            foreach (KHACHHANG item in query) 
            {
                KhachHangDTO dto = new KhachHangDTO();
                Mapper.CreateMap<KHACHHANG, KhachHangDTO>();
                dto = Mapper.Map<KHACHHANG, KhachHangDTO>(item);

                list.Add(dto);
            }
            return list;
        }

        public bool UpdateKhachHang(KhachHangDTO value) {
            try {
                QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();
                var khachHang = (from khItem in context.KhachHangList
                                 where khItem.IDKhachHang == value.IDKhachHang
                                 select khItem).First();
                khachHang.HoTen = value.HoTen;
                khachHang.DiaChi = value.DiaChi;
                khachHang.DienThoai = value.DienThoai;
                khachHang.Email = value.Email;
                khachHang.TienNo = value.TienNo;

                context.SaveChanges();
                return true;
            }
            catch {
                return false;
            }
        }

        public bool UpdateKhachHang(List<KhachHangDTO> khachHangList) {
            try {
                QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();
                foreach (KhachHangDTO item in khachHangList) {
                    if (!UpdateKhachHang(item))
                        return false;
                }

                return true;
            }
            catch {
                return false;
            }
        }

        public int GetDebt(int khachHangID) {
            try {
                QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();
                var debt = (from khItem in context.KhachHangList
                           where khItem.IDKhachHang == khachHangID
                           select khItem.TienNo).First();

                return Convert.ToInt32(debt);
            }
            catch {
                return -1;
            }            
        }

        /// <summary>
        /// tao ra ma khach hang
        /// </summary>
        /// <returns></returns>
        public string CreateMaKhachHang() {             
            QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();
            //indentify ID
            var maxID = (from max in context.GetIndentity("KHACHHANG")
                         select max.CurrentIndentity).FirstOrDefault();
            if (maxID == 1)
                maxID = 0;
            
            return "KH" + (maxID + 1);                    
        
        }

        /// <summary>
        /// tham vao 1 khach hang moi
        /// </summary>
        /// <param name="khachHang"></param>
        /// <returns></returns>
        public bool InsertNewKhachHang(KhachHangDTO khachHang) {
            try {
                QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();
                KHACHHANG kh = new KHACHHANG();
                Mapper.CreateMap<KhachHangDTO, KHACHHANG>();
                kh = Mapper.Map<KhachHangDTO, KHACHHANG>(khachHang);

                context.AddToKhachHangList(kh);
                context.SaveChanges();
                return true;
            }
            catch {
                return false;
            }
        }

        public List<KhachHangDTO> SearchKhachHang(string value) {            
            QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();

            var query = from item in context.KhachHangList
                        where item.DiaChi.Contains(value) || item.DienThoai.Contains(value) || item.Email.Contains(value)
                              || item.HoTen.Contains(value) || item.MaKhachHang.Contains(value)
                        select new KhachHangDTO {
                            IDKhachHang = item.IDKhachHang,
                            MaKhachHang = item.MaKhachHang,
                            TienNo = item.TienNo,
                            DiaChi = item.DiaChi,
                            DienThoai = item.DienThoai,
                            Email = item.Email,
                            HoTen = item.HoTen
                        };

            return query.ToList<KhachHangDTO>();
        }

        public List<KhachHangDTO> SearchKhachHang(string tenKhachHang, string maKhachHang, string diaChi, string soDienThoai, string email) {
            QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();
            var query = from item in context.KhachHangList                        
                        select new KhachHangDTO {
                            IDKhachHang = item.IDKhachHang,
                            MaKhachHang = item.MaKhachHang,
                            HoTen = item.HoTen,
                            DiaChi = item.DiaChi,
                            DienThoai = item.DienThoai,
                            Email = item.Email,
                            TienNo = item.TienNo
                        };

            if (tenKhachHang != "")
                query = query.Where(q => q.HoTen.Contains(tenKhachHang));
            if (maKhachHang != "")
                query.Where(q => q.MaKhachHang.Contains(maKhachHang));
            if (diaChi != "")
                query = query.Where(q => q.DiaChi.Contains(diaChi));
            if (soDienThoai != "")
                query = query.Where(q => q.DienThoai.Contains(soDienThoai));
            if (email != "")
                query = query.Where(q => q.Email.Contains(email));

            return query.ToList<KhachHangDTO>();
        }
    }
}
